//
//  NSString+OASizeExtension.m
//  OASDK_Works
//
//  Created by ChenJie on 3/29/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "NSString+OASizeExtension.h"

@implementation NSString (OASizeExtension)

- (CGSize)oa_sizeWithWidth:(CGFloat)width font:(UIFont *)font {
    return [self oa_sizeWithWidth:width height:CGFLOAT_MAX font:font];
}

- (CGSize)oa_sizeWithWidth:(CGFloat)width font:(UIFont *)font numberOfLines:(NSInteger)numberOfLines {
    
    if (numberOfLines == 0) {
        return [self oa_sizeWithWidth:width font:font];
    }
    
    CGSize singleLineSize = [self oa_singleLineSizeWithWidth:width font:font];
    if (numberOfLines == 1) {
        return singleLineSize;
    }
    
    CGFloat height = singleLineSize.height * numberOfLines;
    return [self oa_sizeWithWidth:width height:height font:font];
}

- (CGSize)oa_singleLineSizeWithWidth:(CGFloat)width font:(UIFont *)font {
    NSString *trimmedString = [[self componentsSeparatedByString:@"\n"] firstObject];
    CGSize lineSize = [trimmedString oa_sizeWithWidth:CGFLOAT_MAX height:CGFLOAT_MAX font:font];
    return CGSizeMake(MIN(lineSize.width, width), lineSize.height);
}

- (CGSize)oa_sizeWithWidth:(CGFloat)width height:(CGFloat)height font:(UIFont *)font {
    return [self boundingRectWithSize:CGSizeMake(width, height)
                              options:NSStringDrawingUsesLineFragmentOrigin
                           attributes:@{NSFontAttributeName: font}
                              context:nil].size;
}


/*
 * size with padding
 */

- (CGSize)oa_sizeWithWidth:(CGFloat)width font:(UIFont *)font padding:(UIEdgeInsets)padding {
    CGSize size = [self oa_sizeWithWidth:width - padding.left - padding.right font:font];
    return CGSizeMake(padding.left + size.width + padding.right, padding.top + size.height + padding.bottom);
}

- (CGSize)oa_sizeWithWidth:(CGFloat)width font:(UIFont *)font numberOfLines:(NSInteger)numberOfLines padding:(UIEdgeInsets)padding {
    CGSize size = [self oa_sizeWithWidth:width - padding.left - padding.right font:font numberOfLines:numberOfLines];
    return CGSizeMake(padding.left + size.width + padding.right, padding.top + size.height + padding.bottom);
}

- (CGSize)oa_singleLineSizeWithWidth:(CGFloat)width font:(UIFont *)font padding:(UIEdgeInsets)padding {
    CGSize size = [self oa_singleLineSizeWithWidth:width - padding.left - padding.right font:font];
    return CGSizeMake(padding.left + size.width + padding.right, padding.top + size.height + padding.bottom);
}



@end
